﻿using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Threading.Tasks;
using Avalonia;

namespace 畜牧业审核程序国产.Models.ToolsClass
{
    /// <summary>
    /// 当page1viewmodel的年度和季度选定后,这个类所有外部可访问的内容全部确定(page1viewmodel里调用了
    /// Global.Reset方法了
    /// </summary>
    internal class Global : ObservableObject
    {
        //[ObservableProperty]
        //private static int intYearNow;
        //[ObservableProperty]
        //private static string? strYearNow;
        //[ObservableProperty]
        //private static int intSeasonNow;
        //[ObservableProperty]
        //private static string? strSeasonNow;

        //[ObservableProperty]
        //private static int intYearPre;
        //[ObservableProperty]
        //private static string? strYearPre;
        //[ObservableProperty]
        //private static int intSeasonPre;
        //[ObservableProperty]
        //private static string? strSeasonPre;

        //[ObservableProperty]
        //private static int intYearYOY;
        //[ObservableProperty]
        //private static string? strYearYOY;
        //[ObservableProperty]
        //private static int intSeasonYOY;
        //[ObservableProperty]
        //private static string? strSeasonYOY;

        public static int IntYearNow; public static string? StrYearNow;
        public static int IntSeasonNow; public static string? StrSeasonNow;

        public static int IntYearPre; public static string? StrYearPre;
        public static int IntSeasonPre; public static string? StrSeasonPre;

        public static int IntYearYOY; public static string? StrYearYOY;
        public static int IntSeasonYOY; public static string? StrSeasonYOY;

        public static string? PlatformNow = null;
        public static string? PlatformPre = null;
        public static string? PlatformYOY = null;


        //public static void SetPlatform()
        //{
        //    //MyDBOperator myDBOperator = new MyDBOperator();改用单例模式
        //    MyDBOperator myDBOperator = MyDBOperator.DBOperatorInstance;

        //    List<string> unExistsUnionTablesNow =
        //           myDBOperator.CheckTablesExist(new string[]{ "一套表" + StrYearNow + StrSeasonNow + "A209","一套表" + StrYearNow + StrSeasonNow + "A210",
        //               "一套表" + StrYearNow + StrSeasonNow + "A211","一套表" + StrYearNow + StrSeasonNow + "A212"});
        //    if (unExistsUnionTablesNow.Count == 4)
        //    {
        //        PlatformNow = "统计云";
        //    }
        //    else { PlatformNow = "一套表"; }

        //    List<string> unExistsUnionTablesPre =
        //       myDBOperator.CheckTablesExist(new string[]{ "一套表" + StrYearPre + StrSeasonPre + "A209","一套表" + StrYearPre + StrSeasonPre + "A210",
        //               "一套表" + StrYearPre + StrSeasonPre + "A211","一套表" + StrYearPre + StrSeasonPre + "A212"});
        //    if (unExistsUnionTablesPre.Count == 4)
        //    {
        //        PlatformPre = "统计云";
        //    }
        //    else
        //    {
        //        PlatformPre = "一套表";
        //    }
        //    List<string> unExistsUnionTablesYOY =
        //      myDBOperator.CheckTablesExist(new string[]{ "一套表" + StrYearYOY + StrSeasonYOY + "A209","一套表" + StrYearYOY + StrSeasonYOY + "A210",
        //               "一套表" + StrYearYOY + StrSeasonYOY + "A211","一套表" + StrYearYOY + StrSeasonYOY + "A212"});
        //    if (unExistsUnionTablesYOY.Count == 4)
        //    {
        //        PlatformYOY = "统计云";
        //    }
        //    else { PlatformYOY = "一套表"; }
        //    // return new Tuple<string, string, string>(PlatformNow, PlatformPre, PlatformYOY);
        //}
        //private void SetPlatform(int yearNow, int intSeasonNow)
        private static void SetPlatform()//只有在用户选择的年度季度变化后才需要调用这个方法
        {
            if (IntYearNow <2024 || (IntYearNow ==2024 && IntSeasonNow <=2))
            {
                PlatformNow = "一套表";
                PlatformPre = "一套表";
                PlatformYOY = "一套表";
            }
            else if (IntYearNow==2024 && IntSeasonNow ==3)
            {
                PlatformNow = "统计云";
                PlatformPre = "一套表";
                PlatformYOY = "一套表";
            }
            else if ((IntYearNow == 2024 && IntSeasonNow == 4) || IntYearNow == 2025 && IntSeasonNow == 1)
            {
                PlatformNow = "统计云";
                PlatformPre = "统计云";
                PlatformYOY = "一套表";
            }
            else
            {
                PlatformNow = "统计云";
                PlatformPre = "统计云";
                PlatformYOY = "统计云";
            }
        }


        public static Tuple<string,string,string> GetWithPlatformYearSeason()
        {
            SetPlatform();//要不要放在Reset里面
            var allThreeSeasonsWithYearAndSeasonNames = Tuple.Create(
                PlatformNow + StrYearNow + StrSeasonNow, PlatformPre + StrYearPre + StrSeasonPre,
                PlatformYOY + StrYearYOY + StrSeasonYOY);
            return allThreeSeasonsWithYearAndSeasonNames;



        }

       

        /// <summary>
        /// 设置全局的年度\季度,并重新设置platform
        /// </summary>
        /// <param name="yearNow"></param>
        /// <param name="seasonNow"></param>
        public static void Reset(int yearNow, int seasonNow)
        {
            IntYearNow = yearNow;
            StrYearNow = yearNow.ToString() + "年";
            IntYearYOY = yearNow - 1;
            StrYearYOY = IntYearYOY.ToString() + "年";
            IntSeasonNow = seasonNow;
            StrSeasonNow = ConvertIntToStringOfSeason(IntSeasonNow);
            IntSeasonYOY = IntSeasonNow;
            StrSeasonYOY = ConvertIntToStringOfSeason(IntSeasonYOY);

            if (IntSeasonNow == 1)
            {
                IntSeasonPre = 4;
                StrSeasonPre = ConvertIntToStringOfSeason(IntSeasonPre);
                IntYearPre = IntYearNow - 1;
                StrYearPre = IntYearPre.ToString() + "年";
            }
            else
            {
                IntSeasonPre = IntSeasonNow - 1;
                StrSeasonPre = ConvertIntToStringOfSeason(IntSeasonPre);
                IntYearPre = IntYearNow;
                StrYearPre = IntYearPre.ToString() + "年";
            }
            
            
            SetPlatform();
        }
        private static string ConvertIntToStringOfSeason<T>(T IntSeason)
        {
            if (IntSeason is int intvalue)
            {
                if (intvalue == 1)
                {
                    return "第一季度";
                }
                else if (intvalue == 2)
                {
                    return "第二季度";
                }
                else if (intvalue == 3)
                {
                    return "第三季度";
                }
                else if (intvalue == 4)
                {
                    return "第四季度";
                }
                else
                {
                    return "输入整形参数错误";
                }
            }
            else if (IntSeason is string strValue)
            {
                if (strValue.Equals("1") || strValue.Equals("一") || strValue.Equals("01"))
                {
                    return "第一季度";
                }
                else if (strValue.Equals("2") || strValue.Equals("二") || strValue.Equals("02"))
                {
                    return "第二季度";
                }
                else if (strValue.Equals("3") || strValue.Equals("三") || strValue.Equals("03"))
                {
                    return "第三季度";
                }
                else if (strValue.Equals("4") || strValue.Equals("四") || strValue.Equals("04"))
                {
                    return "第四季度";
                }
                else
                {
                    return "输入字符串参数错误";
                }
            }
            else
            {
                return "数据类型错误";
            }
        }

        public static Window GetMainWindow()
        {
            if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                return desktop.MainWindow;
            }
            return null;
        }

        //以下两个方法没有引用
        //private int GetIntSeason()
        //{
        //    DateTime spring = new DateTime(DateTime.Now.Year, 3, 25);
        //    DateTime summer = new DateTime(DateTime.Now.Year, 6, 25);
        //    DateTime autumn = new DateTime(DateTime.Now.Year, 9, 25);
        //    DateTime winter = new DateTime(DateTime.Now.Year, 12, 25);
        //    if (DateTime.Now >= spring && DateTime.Now < summer)
        //    {
        //        return 1;
        //    }
        //    else if (DateTime.Now >= summer && DateTime.Now < autumn)
        //    {
        //        return 2;
        //    }
        //    else if (DateTime.Now >= autumn && DateTime.Now < winter)
        //    {
        //        return 3;
        //    }
        //    else
        //    {
        //        return 4;
        //    }
        //}
        //public static int GetIntYear()
        //{
        //    //return DateTime.Now.Year - 1;
        //    return DateTime.Now.Year;
        //}




    }
}
